<template>
  <el-drawer
    :title="!dataForm.id ? '新建' : '编辑'"
    :visible.sync="visible"
    :wrapperClosable="false"
    size="800px"
    append-to-body
    class="jsbos-common-drawer"
  >
    <div class="jsbos-flex-main">
      <div class="dynamicForm">
        <el-form
          v-loading="loading"
          ref="elForm"
          :model="dataForm"
          :rules="rules"
          size="small"
          label-width="100px"
          label-position="right"
        >
          <template v-if="!loading">
            <el-col :span="12">
              <el-form-item label="证书名称" prop="name">
                <el-select
                  v-model="dataForm.name"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  filterable
                >
                  <el-option
                    v-for="(item, index) in nameOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.fullName"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证书编号" prop="code">
                <el-input
                  v-model="dataForm.code"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证书类别" prop="category">
                <el-select
                  v-model="dataForm.category"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  filterable
                >
                  <el-option
                    v-for="(item, index) in categoryOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.enCode"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证书等级" prop="grade">
                <el-select
                  v-model="dataForm.grade"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                >
                  <el-option
                    v-for="(item, index) in gradeOptions"
                    :key="index"
                    :label="item.fullName"
                    :value="item.enCode"
                    :disabled="item.disabled"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="证书所属人" prop="certUserId">
                <user-select
                  v-model="dataForm.certUserId"
                  placeholder="请选择"
                  clearable
                >
                </user-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发证单位" prop="certBy">
                <el-input
                  v-model="dataForm.certBy"
                  placeholder="请输入"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="证书扫描件" prop="cover">
                <UploadFz
                  v-model="dataForm.cover"
                  :fileSize="5"
                  :showTip="true"
                  sizeUnit="MB"
                  :limit="5"
                  buttonText="点击上传"
                >
                </UploadFz>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="有效期" prop="expiredType">
                <el-radio-group v-model="dataForm.expiredType" size="small">
                  <el-radio
                    v-for="(item, index) in expiredTypeOptions"
                    :key="index"
                    :label="item.id"
                    :disabled="item.disabled"
                  >
                    {{ item.fullName }}
                  </el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12" v-if="dataForm.expiredType === '0'">
              <el-form-item label="过期日期" prop="expiredDate">
                <el-date-picker
                  v-model="dataForm.expiredDate"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="复审日期" prop="reviewDate">
                <el-date-picker
                  v-model="dataForm.reviewDate"
                  placeholder="请选择"
                  clearable
                  :style="{ width: '100%' }"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="timestamp"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="证书介绍" prop="description">
                <el-input
                  v-model="dataForm.description"
                  placeholder="请输入"
                  :style="{ width: '100%' }"
                  true
                  type="textarea"
                  :autosize="{ minRows: 4, maxRows: 4 }"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </template>
        </el-form>
      </div>
      <div class="drawer-footer">
        <el-button @click="visible = false"> 取 消 </el-button>
        <el-button
          type="primary"
          @click="dataFormSubmit()"
          :loading="btnLoading"
        >
          确 定
        </el-button>
      </div>
    </div>
  </el-drawer>
</template>
<script>
import request from "@/utils/request";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";

export default {
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      btnLoading: false,
      isDetail: false,
      dataForm: {
        resourceType: "90",
        resourceId: "",
        certId: "",
        name: "",
        code: "",
        category: "",
        grade: "",
        certUserId: "",
        certBy: "",
        idCard: "",
        contentJson: "",
        cover: [],
        expiredType: "1",
        expiredDate: "",
        creatorDate: "",
        reviewDate: "",
        description: "",
        creatorUserId: "",
        creatorTime: "",
        enabledMark: "1",
      },
      rules: {
        name: [
          {
            required: true,
            message: "请选择",
            trigger: "change",
          },
        ],
        certUserId: [
          {
            required: true,
            message: "请选择",
            trigger: "click",
          },
        ],
      },
      resourceTypeOptions: [
        { fullName: "课程", id: "10" },
        { fullName: "考试", id: "20" },
        {
          fullName: "专题",
          id: "30",
        },
        { fullName: "项目管理", id: "40" },
        { fullName: "新人培训", id: "50" },
        {
          fullName: "学习地图",
          id: "60",
        },
        { fullName: "资质证书", id: "90" },
      ],
      nameOptions: [],
      categoryOptions: [],
      gradeOptions: [],
      expiredTypeOptions: [
        { fullName: "永久有效", id: "1" },
        { fullName: "有效周期", id: "0" },
      ],
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getNameOptions();
    this.getCategoryOptions();
    this.getGradeOptions();
  },
  mounted() {},
  methods: {
    getNameOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "CertName",
        })
        .then((res) => {
          this.nameOptions = res;
        });
    },
    getCategoryOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "CertificateCategory",
        })
        .then((res) => {
          this.categoryOptions = res;
        });
    },
    getGradeOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "CertGrade",
        })
        .then((res) => {
          this.gradeOptions = res;
        });
    },
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
      this.dataForm.resourceType = "90";
      this.dataForm.expiredType = "1";
      this.dataForm.enabledMark = "1";
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.isDetail = isDetail || false;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          request({
            url: "/api/course/TemsCertUser/" + this.dataForm.id,
            method: "get",
          }).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          this.request();
        }
      });
    },
    request() {
      var _data = this.dataList();
      if (!this.dataForm.id) {
        request({
          url: "/api/course/TemsCertUser",
          method: "post",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      } else {
        request({
          url: "/api/course/TemsCertUser/" + this.dataForm.id,
          method: "PUT",
          data: _data,
        }).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
            duration: 1000,
            onClose: () => {
              this.visible = false;
              this.$emit("refresh", true);
            },
          });
        });
      }
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.cover = JSON.stringify(_data.cover);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.cover = _dataAll.cover ? JSON.parse(_dataAll.cover) : [];
      this.dataForm = _dataAll;
    },
  },
};
</script>
